﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace NuGet.MSBuild.Tools.Helpers
{
    internal class NuGetPathResolver
    {
        public string SolutionPath { get; set; }

        public string GetPath(string fileName)
        {
            string result = null;

            PackageConfigReader reader = new PackageConfigReader();

            var packages = reader.Read(fileName);
            if (packages != null && packages.Any())
            {
                var package = (from o in packages
                               where o.Id == PackageId.NuGetCommandLine
                               select o).SingleOrDefault();
                if (package != null)
                {
                    result = Path.Combine(
                        this.SolutionPath,
                        SpecialFolder.Packages,
                        string.Format("{0}.{1}", package.Id, package.Version),
                        SpecialFolder.Tools);
                }
            }

            return result;
        }
    }
}